jQuery(document).ready(function(){
	//setting times
	$.setDateController();
	
	$('#collapse').click(function(){			//收展
		$('#queryItems').toggle('slow');
		var isCol = $(this).attr('data');
		if (isCol == 'true') {
			$(this).val("收起查询条件");
			$(this).attr('data', 'false');
		} else {
			$(this).val("展开查询条件");
			$(this).attr('data', 'true');
		}
	}).button();
	
	//表格
	var spanTpl = '<span title="{2}" class="{1}">{0}</span>';
	//formatter
	var temp = {};
	var formatFunc = function(data, options, rowObject) {
		var obj = temp[options.rowId] = temp[options.rowId] || {};
		if (data && typeof data == 'object') {
			data.value = new Number(data.value).toFixed(2);
			obj[options.pos] = data;
		}
		else {
			obj[options.pos] = obj[options.pos] || {};
			obj[options.pos].value = data || '';
		}
		data = obj[options.pos];
		if (data && data != "") {
			if (!data.flag) return data.value;
			var clz = $.trim(data.flag) == 'HL' ? 'exceed' : 'exception';
			return $.format(spanTpl, data.value, clz, data.desp);
		}
		return "";
	};
	var unformatFunc = function(data, options, cell) {
		if (data) {
			return data;
		}
		return "";
	};
	
	//setting grid
	$('#searchBtn').button().click(function(){
		//config grid
		var siteId = $('#site').val(), 
			$items = $('#items_' + siteId + ' input'),
			$form = $('#scopeSearch'),
			lastsel, $grid,
			options = {
				caption:"报表",
				reportFormat: true,
				url: $form.attr('action'),
				mtype: $form.attr('method'),
				datatype: "json",
				gridComplete: function() {
					$.genTableTip(flags);
				},
				cellEdit:true,
				cellurl: "/emip/data/" + type + "/update?siteId=" + siteId,
				afterSubmitCell: function(response, rowId, colId, value, rowNum, colNum) {
					console.log(response.responseText);
					value = {value: value, flag: response.responseText};
					return [true, value];
				},
				postData: $.array2obj($form.serializeArray()),
//				onSelectRow: function(id){
//					if(id && id!==lastsel){
//						$grid.jqGrid('restoreRow',lastsel);
//						$grid.jqGrid('editRow',id,true);
//						lastsel=id;
//					}
//				},
//				ajaxRowOptions: {
//					beforeSend: function(a, b, c, d, e, f, g) {
//						console.log(a);
//					}
//				},
				editurl: "/emip/data/" + type + "/update?siteId=" + siteId
			}, headerNames = [{
				startColumnName: 'time', numberOfColumns: 1, titleText: '时间'
			}], colNames = [""],
			colModel = [{name:'time',index:'time',align:'center',sortable:false}], 
			colIndexes = [];
		
		$items.each(function(index, item){
			var $item = $(item);
			headerNames.push({
				startColumnName:$item.attr("item"), 
				numberOfColumns:1, 
				titleText:item.title
			});
			colNames.push($item.attr("unit"));
			colIndexes.push($item.attr("item"));
			colModel.push({
				name: $item.attr('item'), 
				index: $item.attr('item'), 
				width: '68px',
				align:'center',
				sortable:false,
				formatter: formatFunc,
				unformat: unformatFunc,
				editable:true
			});
		});
		
		options = $.extend(options, {
			groupHeaders: headerNames,
			colNames: colNames,
			colModel: colModel
		});
		
		$grid = $('#grid').genGrid(options);
	});
	
	$('#site').change(function(){			//站点下拉框
		$('#searchBtn').click();
	});
	$('#site').change();
});